package com.example.back.controller;

import com.example.back.util.GetDays;
import com.example.back.common.Result;
import com.example.back.entity.InStore;
import com.example.back.entity.OutStore;
import com.example.back.entity.Store;
import com.example.back.service.InStoreService;
import com.example.back.service.OutStoreService;
import com.example.back.service.StoreService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/OutStore")
public class OutStoreController {
    @Resource
    private OutStoreService outStoreService;
    @Resource
    private StoreService storeService;

    @Resource
    private InStoreService inStoreService;

    @GetMapping("/find")
    public Result find() {
        return Result.success(outStoreService.list());

    }


    @PostMapping("/get")
    public Result get(@RequestBody OutStore outStore) {
        try {
            List<OutStore> outStoreList = new ArrayList<>(5);
            if (outStoreService.getById(outStore.getOutId()) != null) {
                outStoreList.add(outStoreService.getById(outStore.getOutId()));
                return Result.success(outStoreList, "查找成功");
            } else {
                return Result.error("查找失败");
            }
        } catch (Exception ex) {
            return Result.error("查找失败ERROR");
        }
    }

    @PostMapping("/add")
    public Result add(@RequestBody OutStore outStore) {
        try {
            List<Store> all = storeService.list();
            for (Store store : all) {
                if (outStore.getOutStoreId().equals(store.getStoreId())) {
                    if (store.getStoreTon() < outStore.getOutTon()) {
                        return Result.error("仓库剩余吨数不足");
                    } else if (store.getStoreCount() < outStore.getOutCount()) {
                        return Result.error("仓库剩余件数不足");
                    }
                }
            }
            if (outStoreService.getById(outStore.getOutId()) != null) {
                return Result.error("请勿重复出库");
            } else if (storeService.getById(outStore.getOutStoreId()) == null) {
                return Result.error("请检查仓库是否存在");
            }
            double Cost = calculateCost(outStore);
            outStore.setOutCost(Cost);
            return Result.success(outStoreService.save(outStore), "出库成功");

        } catch (Exception ex) {
            return Result.error("出库失败ERROR，违反主键约束，请输入其他ID");
        }

    }


    @PostMapping("/delete")
    public Result delete(@RequestBody OutStore outStore) {
        try {
            if (outStoreService.getById(outStore.getOutId()) != null) {
                return Result.success(outStoreService.removeById(outStore.getOutId()), "删除成功");

            } else {
                return Result.error("删除失败");
            }
        } catch (Exception ex) {
            return Result.error("删除失败ERROR");
        }

    }

    @PostMapping("/modify")
    public Result modify(@RequestBody OutStore outStore) {
        List<Store> all = storeService.list();
        if (outStoreService.getById(outStore.getOutId()) == null || outStoreService.list().size() == 0 || storeService.getById(outStore.getOutStoreId()) == null) {
            return Result.error("修改失败");
        } else {
            return Result.success(outStoreService.updateById(outStore), "修改成功");
        }

    }



    public double calculateCost(OutStore outStore) {
        List<InStore> inStoreList = inStoreService.findAllByStoreId(outStore.getOutStoreId());
        double cost = 0;
        double outTon = outStore.getOutTon();

        for (InStore inStore : inStoreList) {
            if (outTon == 0) {
                break;
            }

            double realTon = inStore.getRealTon();

            if (realTon == 0) {
                continue;
            }

            if (realTon >= outTon) {
                double temp = outTon;
                realTon = realTon - temp;
                outTon = 0;

                long day = GetDays.getdays(outStore.getOutDate(),inStore.getInDate());

                if (day >= 15) {
                    cost = cost + temp * 0.5 * (day - 14);
                }
            } else {
                long day = GetDays.getdays(outStore.getOutDate(),inStore.getInDate());

                if (day >= 15) {
                    System.out.println(storeService.getById(inStore.getInStoreId()).getStoreFree());
                    cost = cost + realTon * 0.5 * (day - 14);
                }

                outTon = outTon - realTon;
                realTon = 0;
            }

            inStore.setRealTon(realTon);
            inStoreService.updateRealTon(inStore.getInId(), realTon);
        }

        return cost;
    }
}
